/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.Artist;
import Model.Category;
import Model.Categorymappings;
import Model.Products;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;

/**
 *
 * @author gulben
 */
@ManagedBean(name = "mainpageController")
@RequestScoped
public class MainpageController implements Serializable {

    private MenuModel bookmodel;
    private MenuModel cdmodel;
    boolean menu = true;
    EntityManager entityManager = Persistence.createEntityManagerFactory("KitapcdappPU").createEntityManager();
    HashMap<Integer, String[]> bookSubCategories = new HashMap<Integer, String[]>();
    HashMap<Integer, String[]> cdSubCategories = new HashMap<Integer, String[]>();

    public MainpageController() {
    }

    public MenuModel getBookModel() {
        
        bookmodel = new DefaultMenuModel();
        Query allParentCategories = entityManager.createNativeQuery("select distinct parentcategoryid from Categorymappings");
        List<Integer> parentCategoryids = (List<Integer>) allParentCategories.getResultList();
        Query allParentCategories2 = entityManager.createNativeQuery("select distinct categoryid from Categorymappings where parentcategoryid=2");
        List<Integer> parentCategoryids2 = (List<Integer>) allParentCategories2.getResultList();
        Query allParentCategories3 = entityManager.createNativeQuery("select distinct parentcategoryid from Categorymappings where parentcategoryid=2");
        List<Integer> parentCategoryids3 = (List<Integer>) allParentCategories3.getResultList();
        parentCategoryids.removeAll(parentCategoryids2);
        parentCategoryids.removeAll(parentCategoryids3);
        for (Integer i : parentCategoryids) {
            if(i != 1) {
                DefaultSubMenu firstSubmenu = new DefaultSubMenu(categoryName(i));
                List<Integer> categories = categoryByParentCategory(i);
                for(Integer j: categories)
                {
                    DefaultMenuItem item = new DefaultMenuItem(categoryName(j));
                    item.setId(j.toString());
                    item.setOnclick("window.open ('/ktapcdapp/faces/productList.xhtml?"
                            + "categoryId=" +item.getId()+ "','_self',false)");
                    firstSubmenu.addElement(item);

                }
                bookmodel.addElement(firstSubmenu);
            }
        }
        return bookmodel;
    }

    public String categoryName(int id) {
        Query category = entityManager.createNamedQuery("Category.findById");
        category.setParameter("id", id);
        Category model = (Category)category.getSingleResult();
        String categoryName = model.getName();
        return categoryName;
    }

    public List<Integer> categoryByParentCategory(int id) {
        Query category = entityManager.createNativeQuery("select distinct categoryid from Categorymappings where parentcategoryid =" + id);
        List<Integer> model = (List<Integer>)category.getResultList();
        return model;
    }

    public MenuModel getCDModel() {
        cdmodel = new DefaultMenuModel();
        Query allParentCategories = entityManager.createNativeQuery("select distinct parentcategoryid from Categorymappings");
        List<Integer> parentCategoryids = (List<Integer>) allParentCategories.getResultList();
        Query allParentCategories2 = entityManager.createNativeQuery("select distinct categoryid from Categorymappings where parentcategoryid=1");
        List<Integer> parentCategoryids2 = (List<Integer>) allParentCategories2.getResultList();
        Query allParentCategories3 = entityManager.createNativeQuery("select distinct parentcategoryid from Categorymappings where parentcategoryid=1");
        List<Integer> parentCategoryids3 = (List<Integer>) allParentCategories3.getResultList();
        parentCategoryids.removeAll(parentCategoryids2);
        parentCategoryids.removeAll(parentCategoryids3);
        for (Integer i : parentCategoryids) {
            if(i != 2) {
                DefaultSubMenu firstSubmenu = new DefaultSubMenu(categoryName(i));
                List<Integer> categories = categoryByParentCategory(i);
                for(Integer j: categories)
                {
                    DefaultMenuItem item = new DefaultMenuItem(categoryName(j));
                    item.setId(j.toString());
                    item.setOnclick("window.open ('/ktapcdapp/faces/productList.xhtml?"
                            + "categoryId=" +item.getId()+ "','_self',false)");
                    firstSubmenu.addElement(item);

                }
                cdmodel.addElement(firstSubmenu);
            }
        }
        return cdmodel;
    }

    public List<Products> getProducts() {
        EntityManager entityManager = Persistence.createEntityManagerFactory("KitapcdappPU").createEntityManager();
        Query query = entityManager.createNamedQuery("Products.findAll");
        List<Products> products = (List<Products>) query.getResultList();
        return products;
    }

    public List<Artist> getArtists(Products productId) {
        EntityManager entityManager = Persistence.createEntityManagerFactory("KitapcdappPU").createEntityManager();
        Query query = entityManager.createNamedQuery("Artistproductmappings.findProductId");
        query.setParameter("productId", productId);
        List<Artist> artists = (List<Artist>) query.getResultList();
        return artists;
    }
}
